package com.llyb.tingshu.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llyb.tingshu.account.mapper.UserAccountDetailMapper;
import com.llyb.tingshu.account.mapper.UserAccountMapper;
import com.llyb.tingshu.account.service.UserAccountService;
import com.llyb.tingshu.common.constant.SystemConstant;
import com.llyb.tingshu.common.util.Decimal2Serializer;
import com.llyb.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llyb.tingshu.model.account.UserAccountDetail;
import com.llyb.tingshu.vo.account.AccountDeductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	@Override
	public void register(Long userId) {
		BigDecimal tempNum = new BigDecimal("100");
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(tempNum);
		userAccount.setAvailableAmount(tempNum);
		userAccount.setLockAmount(new BigDecimal("0"));
		userAccount.setTotalIncomeAmount(new BigDecimal("0"));
		userAccount.setTotalPayAmount(new BigDecimal("0"));
		userAccountMapper.insert(userAccount);
	}

	@Override
	public BigDecimal getAvailableAmount(Long userId) {
		UserAccount userAccount = this.getUserAccountByUserId(userId);
		return userAccount.getAvailableAmount();
	}

	@Override
	public UserAccount getUserAccountByUserId(Long userId) {
		return this.getOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean checkAndDeduct(AccountDeductVo accountDeductVo) {

		int row = userAccountMapper.checkAndDeduct(accountDeductVo.getUserId(),accountDeductVo.getAmount());
		if (row > 0) {
			return true;
		}
		//  将用户的资金流向记录到user_account_detail!
		this.addUserAccountDetail(accountDeductVo.getUserId(),accountDeductVo.getContent(), SystemConstant.ACCOUNT_TRADE_TYPE_MINUS, accountDeductVo.getAmount(), accountDeductVo.getOrderNo());
		return false;
	}

	public void addUserAccountDetail(Long userId, String content, String accountTradeTypeMinus, BigDecimal amount, String orderNo) {
		//  创建对象
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(content);
		userAccountDetail.setTradeType(accountTradeTypeMinus);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		//  保存数据；
		userAccountDetailMapper.insert(userAccountDetail);
	}

	@Override
	public IPage<UserAccountDetail> findUserRechargePage(Page<UserAccountDetail> pageParam, Long userId) {
		return userAccountDetailMapper.selectPage(pageParam,
				new LambdaQueryWrapper<UserAccountDetail>()
						.eq(UserAccountDetail::getUserId, userId)
						.eq(UserAccountDetail::getTradeType, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT));
	}

	@Override
	public IPage<UserAccountDetail> findUserConsumePage(Page<UserAccountDetail> objectPage, Long userId) {
		return userAccountDetailMapper.selectPage(objectPage,
				 new LambdaQueryWrapper<UserAccountDetail>()
						 .eq(UserAccountDetail::getUserId, userId)
						 .eq(UserAccountDetail::getTradeType, SystemConstant.ACCOUNT_TRADE_TYPE_MINUS));
	}
}
